package com.buer.storage.controller;

import com.buer.storage.service.StockdetailsService;
import com.buer.storage.utils.CheckEmptyUtil;
import com.buer.storage.utils.PdfMerge;
import com.buer.storage.utils.Zpl2Pdf;
import com.buer.storage.vo.R;
import com.buer.storage.vo.StockDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static com.buer.storage.utils.Font2ZplGF.getFontHex;

/**
 * Created by Administrator on 2021/2/8.
 */
@RestController
@RequestMapping("/zebra")
public class ZebraController {
    private static final String QT = "${QT}";
    private static final String NAME = "${NAME}";
    private static final String NAMEVALUE = "${NAMEVALUE}";

    private static final String CODE = "${CODE}";
    private static final String CODEVALUE = "${CODEVALUE}";

    private static final String AREA = "${AREA}";
    private static final String AREAVALUE = "${AREAVALUE}";

    private static final String DEPART = "${DEPART}";
    private static final String DEPARTVALUE = "${DEPARTVALUE}";
    private static final String url = "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/";

    @Autowired
    private StockdetailsService stockdetailsService;

    @PostMapping("/getZebraTemplate")
    public R getZebraTemplate(@RequestBody List<Integer> stockDetails) throws IOException {
        if(CheckEmptyUtil.isNotEmpty(stockDetails)){
            String zpl = getZplTemplate();
            List<InputStream> streamList = getStreamList(zpl,stockDetails);
            byte[] bytes = PdfMerge.mergePdfFiles(streamList);
            //String url = OssUtil.upload("资产模板.pdf", bytes);
           // System.out.println(url);
            FileOutputStream fileOutputStream = new FileOutputStream("D:/common/Test4.pdf");
            fileOutputStream.write(bytes);

            return R.ok();
        }
        return R.fail();
    }

    public List<InputStream> getStreamList(String zpl,List<Integer> stockDetails){
        java.util.List<InputStream> inputStreams=new ArrayList<>();

        for (Integer stockDetailsId:stockDetails){
            StockDetailVo stockDetailVo = stockdetailsService.getStockdetailsById(stockDetailsId);
            String namevalue = getFontHex(stockDetailVo.getProdName(), 210, 240, 20, 0);
            String codevalue = getFontHex(stockDetailVo.getRfid(), 210, 270, 20, 0);
            String areavalue = getFontHex(stockDetailVo.getAreaName(), 210, 300, 20, 0);
            String departvalue = getFontHex(stockDetailVo.getDepartmentCode(), 210, 330, 20, 0);

            String zplnew = zpl.replace(QT,stockDetailVo.getRfid())
                    .replace(NAMEVALUE, namevalue)
                    .replace(CODEVALUE, codevalue)
                    .replace(AREAVALUE, areavalue)
                    .replace(DEPARTVALUE, departvalue);
            InputStream in = Zpl2Pdf.send(url, zplnew);
            inputStreams.add(in);

        }
        return inputStreams;
    }


    public  String getZplTemplate(){
        String rwmZpl="\u0010CT~~CD,~CC^~CT~\n" +
                "^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR6,6~SD15^JUS^LRN^CI0^XZ\n" +
                "^XA\n" +
                "^MMT\n" +
                "^PW400\n" +
                "^LL0400\n" +
                "^LS0\n" +
                "^FT127,228^BQN,2,7\n" +
                "^FH\\^FDLA,${QT}^FS\n" +
                "^FT77,253^A0N,14,14^FH\\^FD${NAME}^FS\n" +
                "^FT76,293^A0N,14,14^FH\\^FD${CODE}^FS\n" +
                "^FT75,327^A0N,14,14^FH\\^FD${AREA}^FS\n" +
                "^FT77,362^A0N,14,14^FH\\^FD${DEPART}^FS\n" +
                "^FT195,251^A0N,14,14^FH\\^FD${NAMEVALUE}^FS\n" +
                "^FT195,291^A0N,14,14^FH\\^FD${CODEVALUE}^FS\n" +
                "^FT195,326^A0N,14,14^FH\\^FD${AREAVALUE}^FS\n" +
                "^FT195,360^A0N,14,14^FH\\^FD${DEPARTVALUE}^FS\n" +
                "^PQ1,0,1,Y^XZ\n";

        String name = getFontHex("装备名称：", 110, 240, 20, 0);
       // String namevalue = getFontHex("消防水袋", 210, 240, 20, 0);

        String code = getFontHex("装备编号：", 110, 270, 20, 0);
        //String codevalue = getFontHex("ch123456", 210, 270, 20, 0);

        String area = getFontHex("装备库区：", 110, 300, 20, 0);
        //String areavalue = getFontHex("灭火药剂类", 210, 300, 20, 0);

        String depart = getFontHex("装备库位：", 110, 330, 20, 0);
       // String departvalue = getFontHex("A1001", 210, 330, 20, 0);
        //.replace(QT,"qt123456")
        //.replace(NAMEVALUE,namevalue)
        //.replace(CODEVALUE,codevalue)
        //.replace(AREAVALUE,areavalue)
        //.replace(DEPARTVALUE,departvalue)
        String zpl=rwmZpl
                .replace(NAME,name)
                .replace(CODE,code)
                .replace(AREA,area)
                .replace(DEPART,depart);

        System.out.println(zpl);

        return zpl;
    }
}
